<?php

// TODO: review

if (preg_match('/^[http|https]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[#~A-Za-z0-9\.\/%&=\?\-_:\+]+$/i',$_GET['url'])) {
  $url = urlencode($_GET['url']);
}

$urlPerformance = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=' . $url .'&key=TODO';
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$urlPerformance);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_REFERER,'');
curl_setopt($curl_handle,CURLOPT_USERAGENT,'');
$bufferPerformanceTemp = curl_exec($curl_handle);
$findPerformanceScoreContainer = preg_match('/\"score\"\: \d+/', $bufferPerformanceTemp, $performanceScoreContainer);
if ($findPerformanceScoreContainer = true) {
  $findPerformanceScore = preg_match('/\d+/', htmlentities($performanceScoreContainer['0']), $performanceScore);
  $bufferPerformance = round(htmlentities($performanceScore['0'])/10);
  echo $bufferPerformance;
} else {
  echo "n/a";
}

curl_close($curl_handle);

?>